﻿using DevExpress.CrmDemo.Win.Data;
using System.Linq;
using System.Windows.Forms;
using System;
using DevExpress.CrmDemo.Win.Helpers;

namespace DevExpress.CrmDemo.Win {
    public partial class OpportunitiesEditorForm : DevExpress.XtraEditors.XtraForm {
        #region Constructor
        public OpportunitiesEditorForm() {
            InitializeComponent();
            labelControl10.ForeColor = ColorHelper.LabelColorLight;
            labelControl1.ForeColor = ColorHelper.LabelColorLight;
            labelControl2.ForeColor = ColorHelper.LabelColorLight;
            labelControl3.ForeColor = ColorHelper.LabelColorLight;
            labelControl4.ForeColor = ColorHelper.LabelColorLight;
            labelControl5.ForeColor = ColorHelper.LabelColorLight;
            labelControl6.ForeColor = ColorHelper.LabelColorLight;
            labelControl7.ForeColor = ColorHelper.LabelColorLight;
            ColorHelper.SetMenuManager(Controls, this.LookAndFeel.ActiveSkinName, this);
        }

        public OpportunitiesEditorForm(Opportunity opportunity)
            : this() {
            this.opportunityBindingSource.DataSource = opportunity;

            DataBindingHelpers.SuspendTwoWayBinding(this.BindingContext[opportunityBindingSource]);
        }
        #endregion

        public bool GoToDetail { get; set; }

        public event NavRequestEventHandler NavRequest;

        protected void OnNavRequest(NavRequestEventArgs args) {
            if (NavRequest != null)
                NavRequest(this, args);
        }

        #region Event Handlers

        private void simpleButtonClose_Click(object sender, EventArgs e) {
            this.DialogResult = DialogResult.Cancel;
            Close();
        }

        private void simpleButtonSave_Click(object sender, EventArgs e) {
            DataBindingHelpers.UpdateDataBoundObject(this.BindingContext[opportunityBindingSource], opportunityBindingSource);

            if (this.Validate()) {
                this.DialogResult = DialogResult.OK;
                Close();
            }
        }

        private void simpleButtonDetails_Click(object sender, EventArgs e) {
            GoToDetail = true;
            this.DialogResult = DialogResult.Cancel;
            Close();
        }

        private void OpportunitiesEditorForm_Load(object sender, System.EventArgs e) {
            this.stagesBindingSource.DataSource = DataProvider.Instance.Stages;
            this.teamMemberBindingSource.DataSource = DataProvider.Instance.TeamMembers;
            this.comboBoxEditCategory.Properties.Items.AddRange(DataProvider.Instance.Technologies.ToArray());
        }

        #endregion
    }
}
